package com.yileiqian.alg;

import java.util.Random;

public class Sudoku {

	private int[][] matrix;
	private static final int SIZE = 9;
	private Random random = new Random();

	public Sudoku() {
		matrix = new int[9][9];
		init();
	}

	private void init() {
		for (int i = 0; i < 17; i++) {
			int x = random.nextInt(9);
			int y = random.nextInt(9);

			if (matrix[x][y] == 0) {
				matrix[x][y] = random.nextInt(9) + 1;
			} else {
				i--;
			}
		}
	}
	
	public boolean checkNumber(){
		return false;
	}

	public boolean check(){
		int[] h = new int[9];
		int[] v = new int[9];
		int[] c = new int[9];
		
		for(int y = 0; y < 9; y++){
			for(int x = 0; x < 9; x++){
				if(matrix[x][y] > 1 && matrix[x][y] < 10){
					h[y] |= 1 << (matrix[x][y] - 1);
					v[x] |= 1 << (matrix[x][y] - 1);
					c[y/3*3+x/3] |= 1 << (matrix[x][y] - 1);
				}
			}
		}
		
		for(int i = 0; i < 9; i++){
			if(h[i] != 1023){
				return false;
			}
			if(h[i] != 1023){
				return false;
			}
			if(h[i] != 1023){
				return false;
			}
		}
		return true;
	}

	public String toString() {
		StringBuffer buffer = new StringBuffer();

		String line = " - - - - - - - - -\n";

		for (int y = 0; y < 9; y++) {
			if (y % 3 == 0) {
				buffer.append(line);
			}

			for (int x = 0; x < 9; x++) {

				if (x % 3 == 0) {
					buffer.append('|');
				} else {
					buffer.append(' ');
				}
				if (matrix[x][y] > 0 && matrix[x][y] <= 9) {
					buffer.append(matrix[x][y]);
				} else {
					buffer.append(' ');
				}
			}
			buffer.append("|\n");

		}
		buffer.append(line);

		return buffer.toString();
	}

	public static void main(String args[]) {
		System.out.println(new Sudoku());
	}
}
